<?

session_start();

require("../../classCol.php");
require("../../models/GFetchers.php");
require("../../models/GHandlers.php");

require("../models/handlers.php");

define('_NOT_BALANCE'    , "~**~");

$srcDbHdr       = "PCH_RET_MANUAL_SER_NUMBER_HDR";
$srcDbDtl       = "PCH_RET_MANUAL_SER_NUMBER_DTL";
$srcDbSequence  = "MST_SEQUENCE";

$CFetchers  = new CGFetchers;
$CHandlers  = new CGHandlers;
$COracle    = new oracleDb;

$COracle->init($_SESSION["ORCL"], 
               $_SESSION["OPASS"],
               $_SESSION["SID"],
               _NO_COMMENT);


$srcJobId = $_POST["srcJid"];
$srcInput = explode("\n", 
                    $_POST["srcInput"]);
$srcDtlNo = $_POST["DTL_NO"];
$srcSubNo = $_POST["SUB_NO"];

$maxInput = sizeof($srcInput);

$thisSequence = "RET_MAN_SER_NUMBER_SEQ";

if($_POST["srcQuantity"] == sizeof($srcInput)){
  
  if($COracle->connect()){
  
    /**
    * Generate JobId
    *
    */
    $thisPeriode = &$_SESSION["sysPeriode"];
    	
    /**
    * Fetch the sequence number
    * and below is the how to
    * 
    * 1. we fetch the current number
    * 2. we update the current number
    * 3. if the fetched number as condition is  
    *    valid then there is no interuption while
    *    updating
    *
    */
    
    $seqEditStatus = false;
    
    while($seqEditStatus == false){
      
    	$CFetchers->fetchCurSequence($COracle,
                  	               $thisSequence,
                  	               $thisPeriode,
                  	               $srcDbSequence,
                  	               $srcJidIndex);
    	
    	$CHandlers->initCurSequence($COracle,
                  	              $thisSequence,
                  	              $srcJidIndex,
                  	              $thisPeriode,
                  	              $srcDbSequence,
                  	              $seqEditStatus);
    }
    
    /**
    * Extract the system period
    *
    */
    $tmpDate = explode("-", $thisPeriode);
    $wrkDate = $tmpDate[0]. /* This period month */
               "-".
               $tmpDate[1]; /* This period year */
    
    $thisJobId  = addDigit((string) $srcJidIndex). 
                  $srcJidIndex. 
                  "/" . 
                  $wrkDate;
    
    
    /**
    * insert into DB
    *
    */
    for($idx = 0; $idx < $maxInput; $idx++){
      
      insMiniInputDtl($COracle,
                      $thisJobId,
                      $srcInput[$idx],
                      $srcDbDtl);
    }
    
    insMiniInputHdr($COracle,
                    $thisJobId,
                    $srcDtlNo,
                    $srcSubNo,
                    $srcDbHdr);
    
    $COracle->commit();
    
    if($COracle->getStatus()){
  	
  	  echo  $thisJobId;
  	}else{
  	  
  	  echo  _RET_NULL;
  	}
    
    $COracle->dc();
  }else{
    
    echo  _CONN_ERROR;
  }
}else{
  
  echo  _NOT_BALANCE;
}
?>